import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'

export default ({ mode }) => {
  const env = loadEnv(mode, process.cwd())
  const define =
    env.VITE_USER_NODE_ENV === 'development' ? { global: {} } : null

  return defineConfig({
    base: './',
    define,
    plugins: [
      vue(),
      Components({
        resolvers: [VantResolver()],
      }),
    ],
    build: {
      outDir: 'build',
    },
    resolve: {
      alias: {
        '@': `${__dirname}/src`,
        comps: `${__dirname}/src/components`,
        img: `${__dirname}/src/assets/img`,
        style: `${__dirname}/src/assets/style`,
        js: `${__dirname}/src/assets/js`,
      },
    },
    server: {
      host: '0.0.0.0',
      port: 5001,
      proxy: {
        '/api': {
          target: env.VITE_API_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    },
  })
}
